package com.server.common.logistics.factory;

import com.server.common.code.error.BusinessException;
import com.server.common.code.utils.OptionalUtils;
import com.server.common.logistics.enums.LogisticsCompanyEnum;
import com.server.common.logistics.interfaces.LogisticsFactoryInterface;
import com.server.common.logistics.interfaces.LogisticsService;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件服务器工厂
 * @author LXC
 * @date 2022/6/4 2:33
 */
@Component
public class LogisticsFactory implements LogisticsFactoryInterface {

    private static final Map<String, LogisticsService> logisticsInterfaceFactory = new HashMap<>();

    @Override
    public LogisticsService getLogisticsInterface(LogisticsCompanyEnum logisticsCompanyEnum){
        LogisticsService logisticsService = logisticsInterfaceFactory.get(logisticsCompanyEnum.getName());
        OptionalUtils.ofEmpty(logisticsService).orElseThrow(()->new BusinessException("暂无不支持此物流"));
        return logisticsService;
    }

    /**
     * 注册
     */
    public void register(LogisticsCompanyEnum logisticsCompanyEnum, LogisticsService logisticsService) {
        if (!ObjectUtils.isEmpty(logisticsCompanyEnum)) {
            logisticsInterfaceFactory.put(logisticsCompanyEnum.getName(), logisticsService);
        }
    }

}
